#ifndef BONJOURSERVICERESOLVER_H
#define BONJOURSERVICERESOLVER_H

#include <QtCore/QObject>
#include <QSocketNotifier>
#include <QHostInfo>

#ifdef _WIN32
	typedef unsigned short UINT16;
	typedef unsigned int UINT18;
	typedef unsigned char UINT8;
	typedef signed char INT8;
	typedef short INT16;
#endif

#include <dns_sd.h>

#include "../Common/bonjourrecord.h"


/** Class allows Bonjour services to be resolved given the descriptor record. */
class BonjourServiceResolver : public QObject
{
	Q_OBJECT

	public:
		BonjourServiceResolver(QObject *parent)	: QObject(parent),
				       dnssref(0), bonjourSocket(0), bonjourPort(-1) {}

		~BonjourServiceResolver() { cleanupResolve(); }

		/** Resolve a Bonjour record. */
		void resolveBonjourRecord(const BonjourRecord &record);

	signals:
		void bonjourRecordResolved(const QHostInfo &hostInfo, int port);
		void error(DNSServiceErrorType error);

	private:
		static void DNSSD_API bonjourResolveReply(DNSServiceRef sdRef,
							  DNSServiceFlags flags,
							  quint32 interfaceIndex,
							  DNSServiceErrorType errorCode,
							  const char *fullName,
							  const char *hostTarget,
							  quint16 port,	quint16 txtLen,
							  const char *txtRecord,
							  void *context);

		DNSServiceRef dnssref;
		QSocketNotifier *bonjourSocket;
		int bonjourPort;

	private slots:
		void bonjourSocketReadyRead();
		void cleanupResolve();
		void finishConnect(const QHostInfo &hostInfo);
};

#endif // BONJOURSERVICERESOLVER_H
